package com.sign2024.sign2024project.operlog.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.common.utils.TimeFormatUtils;
import com.sign2024.sign2024core.entry.sysoperlog.SysOperLog;
import com.sign2024.sign2024project.operlog.mapper.SysOperLogMapper;
import com.sign2024.sign2024project.operlog.param.SysOperLogParam;
import com.sign2024.sign2024project.operlog.service.SysOperLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2024/7/18 10:09
 */
@Service
public class SysOperLogServiceImpl implements SysOperLogService {

    @Resource
    private SysOperLogMapper sysOperLogMapper;

    @Override
    public Page<SysOperLog> pageSysOperLog(SysOperLogParam param) {
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 处理时间参数
        if (StringUtils.isNotNull(param.getOperTime())) {
            String monthDay = TimeFormatUtils.formatStrToYearMonthDay(param.getOperTime());
            param.setOperTime(monthDay);
        }

        // 分页查询
        Page<SysOperLog> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<SysOperLog> sysOperLogs = sysOperLogMapper.pageOperLogList(param);

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
//        Long aLong = sysOperLogMapper.selectCount(null);
        Long aLong = sysOperLogMapper.pageCount(param);

        page.setTotal(aLong);

        page.setRecords(sysOperLogs);

        return page;
    }

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog) {
        sysOperLogMapper.insert(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
        return sysOperLogMapper.selectOperLogList(operLog);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public void deleteOperLogByIds(Long[] operIds) {
        sysOperLogMapper.deleteOperLogByIds(operIds);
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId) {
        return sysOperLogMapper.selectOperLogById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        sysOperLogMapper.cleanOperLog();
    }

    @Override
    public List<String> selectTitle() {
        return sysOperLogMapper.selectTitle();
    }

    @Override
    public List<String> selectBusinessType() {
        return sysOperLogMapper.selectBusinessType();
    }
}
